*comment Copyright 2010 by Dan Fabulich.
*comment 
*comment Dan Fabulich licenses this file to you under the
*comment ChoiceScript License, Version 1.0 (the "License"); you may
*comment not use this file except in compliance with the License. 
*comment You may obtain a copy of the License at
*comment 
*comment  http://www.choiceofgames.com/LICENSE-1.0.txt
*comment 
*comment See the License for the specific language governing
*comment permissions and limitations under the License.
*comment 
*comment Unless required by applicable law or agreed to in writing,
*comment software distributed under the License is distributed on an
*comment "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
*comment either express or implied.

*if (animals = 0)
	Your most immediate problem is you're effectively stranded without beasts of burden.
	*page_break
	*goto newtown
*else
	*set time (time + ((10-speed)/3))
	
*label newtown
In the next town you find a few merchants and shopkeeps.
*temp spices_price
*temp dyes_price
*temp metals_price
*temp gems_price
*temp metals_cost
*temp gems_cost
*temp rounding
*temp rounded
*set spices_price (20 + (shrewdness * (2/3)))							
*comment ****20-86
*set rounding "spices_price"
*gosub round
*set dyes_price (42 + (shrewdness * (4/5)))								
*comment ****42-122
*set rounding "dyes_price"
*gosub round
*set metals_price (65 + (shrewdness * (3/2)))						
*comment ****65-215
*set rounding "metals_price"
*gosub round
*set gems_price (((750 - shrewdness) / (150 - shrewdness)) * 29)	
*comment ****145-377
*set rounding "gems_price"
*gosub round

*temp more_guards
*set more_guards false
*label town
*choice
	#Buy animals and supplies.
		*temp animalcost
		*set animalcost (200 + (250 * animals))
		Buying new stock and replenishing your supplies will total ${animalcost}. You have ${funds} funds left.
		*choice 
			*if (funds > (animalcost - 1)) #Make it so.
				You purchase new animals and supplies.
				*set funds (funds - animalcost)
				*set animals +1
				*goto town
			#Never mind.
				*goto town
	#Sell goods.
		After some haggling, you settle on the following prices:
		*label prices
		
		*if (spices > 0)
			*line_break
			Spices and incense for ${spices_price} per parcel.
		*if (dyes > 0)
			*line_break
			Cakes of dye and rolls of cloth for ${dyes_price} each.
		*if (metals > 0)
			*line_break
			Blocks of valuable metals, jewelry, and other worked goods for ${metals_price} each.
		*if (gems > 0)
			*line_break
			Precious gems for ${gems_price} per stone.

		You have ${funds} funds left.
		
		*temp category
		What will you sell?
		*choice
			*if (spices > 0) #Spices.
				*set category "spices"
				*goto howmuch
			*if (dyes > 0) #Dyes.
				*set category "dyes"
				*goto howmuch
			*if (metals > 0) #Metals.
				*set category "metals"
				*goto howmuch
			*if (gems > 0) #Gems.
				*set category "gems"
				*goto howmuch
			#Never mind.
				*goto town
	*selectable_if (more_guards = false) #Hire guards.
		Hiring more guards and equipping them properly will cost 700. You have ${funds} funds available.
		*choice
			*if (funds > 699) #Make it so.
				You carefully choose new members of your guard.
				*set more_guards true
				*set funds -700
				*set guards +1
				*goto town
			#Never mind.
				*goto town
	#Move on.
		*if (animals = 0)
			Not without animals, you won't!
			*goto town
		You move on.
		*finish
		
*label howmuch
*temp max
*set max {category} 
And how many of your ${max} ${category} will you sell?

*temp quantity
*temp proceeds
*temp cat_price
*set cat_price category & "_price"
*choice
	#All of them.
		*set quantity max
		
		*label determine_price
		*set rounding "quantity"
		*gosub round
		
		*setref category ({category} - quantity)
		*set proceeds (quantity * {cat_price})
		*set funds +proceeds
		
		You sell ${quantity} ${category} for ${proceeds}.
		*goto prices
	*selectable_if (max > 3) #Most of them.
		*set quantity (max * (2/3))
		*goto determine_price
	*selectable_if (max > 5) #A portion of them.
		*set quantity (max * (1/4))
		*goto determine_price
	#Only one.
		*set quantity 1
		*goto determine_price
	#None.
		*goto prices
		

*label round
*set rounded 0
*temp val
*label roundloop
*set val {rounding}
*if (val - rounded) > 1
	*set rounded +1
	*goto roundloop
*elseif (val - rounded) = 1
	*comment Need to account for rounding an integer value.
	*set rounded +1
	*goto roundfinish
*else
	*label roundfinish
	*setref rounding rounded
	*return